// https://leetcode-cn.com/problems/house-robber-iii/
// 337. House Robber III
import { TreeNode } from "../utils/index";
function rob(root: TreeNode | null): number {
    function dfs(node: TreeNode | null): number[] {
        if (node === null) {
            return [0, 0];
        }

        const l = dfs(node.left);
        const r = dfs(node.right);
        const selected = node.val + l[1] + r[1];
        const unselected = Math.max(l[0], l[1]) + Math.max(r[0], r[1]);
        return [selected, unselected];
    }

    const [selected, unselected] = dfs(root);
    return Math.max(selected, unselected);
}
